package com.guo.test;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;

//import javax.sound.sampled.AudioInputStream;
//import javax.sound.sampled.AudioSystem;
//import javax.sound.sampled.Clip;
import java.io.File;

/**
 * 作者: guo yao
 * 日期: 2015年12月29日
 * 时间: 下午12:01
 * <p>
 * 参考 http://blog.csdn.net/chao121/article/details/8449858
 */
public class VideoDemo {

    /*public void normal1() throws Exception {
//        String str = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload/20160105/18801451964766835.mp3";
        String str = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload\\20160105\\18801451964766835.mp3";
        String str2 = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload\\20160105\\18801451964766835.mp3";
//        File file = new File("I:/tmp/18801451964766835.mp3");
        File file = new File("I:/tmp/test.mp3");
//        File file = new File(str);
        Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        clip.open(ais);
        System.out.println(clip.getMicrosecondLength() / 1000000D + " s");//获取音频文件时长
    }*/

    public static int getMp3TrackLength(String src) {
        try {
            MP3File f = (MP3File) AudioFileIO.read(new File(src));
            MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
            return audioHeader.getTrackLength();
        } catch (Exception e) {
            return -1;
        }
    }

    public void normal2() throws Exception {

        File[] dirs = new File[]{
                new File("I:\\tmp\\test.mp3"),
                new File("I:\\tmp\\test.mp3")
        };

        /*for (File dir : dirs) {
            File[] fs = dir.listFiles();
            for (File file : fs) {
                int length = VideoDemo.getMp3TrackLength(file);
                if (length <= 0) {
                    System.err.println("###出错" + file.getName() + "=" + VideoDemo.getMp3TrackLength(file));
                } else {
                    System.out.println(file.getName() + "=" + VideoDemo.getMp3TrackLength(file));
                }

            }
        }*/
    }

    public static String zhuanHuan(Long time) {
        long hour = time / 3600;    //小时
        long minute = time % 3600 / 60;  //分钟
        long second = time % 60;        //秒
        return hour + ":" + minute + ":" + second;
    }

    public static void main(String[] args) throws Exception {
        String str = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload/20160105/18801451964766835.mp3";
        String str1 = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload\\20160105\\18801451964766835.mp3";
        String str2 = "C:\\idea2\\cms-api-app\\cms-api-app-web\\target\\cms-api-app\\upload\\20160105\\18801451964766835.mp3";
        VideoDemo demo = new VideoDemo();
        System.out.println(VideoDemo.getMp3TrackLength(str));
//        System.out.println(getMp3TrackLength(new File("")));
//        System.out.println(zhuanHuan(Long.valueOf(getMp3TrackLength(new File("")))));
    }
}
